export default (Vue) => {
    return Vue.directive('formItemVerifier', {
        bind(el, binding, vnode) {
            // console.dir(binding.value)
            // 递归找上级
            function upward(el) {
                let parent = el.offsetParent
                let name = parent.className
                // console.log(name)
                while (parent && name.indexOf('el-table__body-wrapper') < 0) {
                    parent = parent.offsetParent
                    if (parent) name = parent.className
                }
                return parent
            }


            // 监听嵌套表的dom变化
            window.m_b_formItem = new MutationObserver(function (mutationRecord, observer) {
                /*console.log(mutationRecord, observer)
                console.log(mutationRecord[0].addedNodes.length)*/
                // console.log(el.className.indexOf('is-error'))
                el.parentNode.style.overflow = 'visible'
                if (el.className.indexOf('is-error') > 0) {
                    el.addEventListener('mouseover', function () {
                        // console.log(el.offsetParent.offsetParent) // el-table__body-wrapper
                        let 外框 = upward(el)
                        if (外框.style.overflow) {
                            外框.style.overflow = ''
                        }
                        let trArr = 外框.getElementsByTagName('tr')
                        if (trArr.length > 1) {
                            let 外框底 = 外框.getBoundingClientRect().bottom
                            let formItem = el.getBoundingClientRect().bottom
                            // console.log(外框底, formItem)
                            if ((外框底 - formItem) < 30 && el.className.indexOf('up') < 0) {
                                el.className = el.className + ' up'
                            } else if (el.className.indexOf('up') > 0) {
                                el.classList.remove('up')
                            }
                        } else {
                            外框.style.overflow = 'visible'
                            if (el.className.indexOf('up') < 0) {
                                el.className = el.className + ' up'
                            }
                        }

                    }, false)
                }
            })
            window.m_b_formItem.observe(
                el, {
                    attributes: true
                })
        }
    })

}
